package com.adderis.soft.domain.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.adderis.soft.domain.dao.IFonctionsDao;
import com.adderis.soft.domain.model.Fonctions;

@Repository("fonctionsDao")
public class FonctionsDaoImpl implements IFonctionsDao {

	private static final Log log = LogFactory.getLog(FonctionsDaoImpl.class);

	Calendar calendar = Calendar.getInstance();
	java.util.Date now = calendar.getTime();
	Timestamp nowtimesstamp = new java.sql.Timestamp(now.getTime());;

	@PersistenceContext
	private EntityManager entityManager;

	/**
	 * {@inheritDoc}
	 */
	public void persist(Fonctions transientFonctions) {
		log.debug("Persisting NomCommerial instance");
		try {
			transientFonctions.setCreatetimestamp(nowtimesstamp);
			transientFonctions.setStatutcode("A");
			entityManager.persist(transientFonctions);

			log.debug("persist successful");

		} catch (RuntimeException re) {
			log.error("persist failed", re);
		}

	}

	public void remove(Fonctions persistentFonctions) {
		log.debug("removing Fonctions instance");
		try {
			persistentFonctions.setStatutcode("D");
			persistentFonctions.setDeletiontimesstamp(nowtimesstamp);
			entityManager.merge(persistentFonctions);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.debug("remove failed", re);
		}

	}

	public void remove(Integer FonctionsId) {
		this.remove(this.findById(FonctionsId));

	}

	public Fonctions merge(Fonctions detachedFonctions) {
		log.debug("merging Fonctions instance");
		try {
			detachedFonctions.setUpdatetimesstamp(nowtimesstamp);
			Fonctions result = entityManager.merge(detachedFonctions);
			entityManager.merge(detachedFonctions);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			return null;
		}
	}

	public Fonctions findById(Integer id) {
		log.debug("getting Fonctions instance with id: " + id);
		try {
			Fonctions instance = entityManager.find(Fonctions.class, id);
			log.debug("findById successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("findById failed", re);
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */

	@SuppressWarnings("unchecked")
	public List<Fonctions> findAll() {
		log.debug("getting all Fonctions instance");
		try {
			Query query = entityManager
					.createQuery("SELECT c FROM Fonctions c");
			List<Fonctions> fonctionsList = (List<Fonctions>) query
					.getResultList();
			log.debug("findAll successful");
			return fonctionsList;

		} catch (RuntimeException re) {
			log.error("findAll failed", re);
			return new ArrayList<Fonctions>();
		}

	}

	@SuppressWarnings("unchecked")
	public List<Fonctions> search(String searchString) {
		log.debug("Search Fonctions instances with search string: "
				+ searchString);
		try {
			Query query = entityManager.createQuery("select nc from Fonctions nc where nc.Fonctions like :searchString");
			query.setParameter("searchString", searchString);
			List<Fonctions> fonctionsList = (List<Fonctions>) query
					.getResultList();
			log.debug("search successful");
			return fonctionsList;
		} catch (RuntimeException re) {
			log.error("search failed", re);
			return new ArrayList<Fonctions>();
		}
	}

}
